<template>
    <div class="app-container">

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['equipment:type:add']">新增</el-button>
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- @selection-change="handleSelectionChange" -->
        <el-table v-loading="loading" :data="typeList" row-key="typeId">
            <el-table-column type="selection" width="60" align="center" v-if="false" />
            <el-table-column label="班组主键id" align="center" prop="typeId" v-if="false" />
            <el-table-column label="故障类型编码" align="center" prop="typeCode" />
            <el-table-column label="故障类型名称" align="center" prop="typeName" />
            <el-table-column label="描述" align="center" prop="remarks" />
            <el-table-column label="等级" align="center" prop="remarks" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-add" style="margin-right: 9px"
                        @click="handleAdd(scope.row)" v-hasPermi="['equipment:type:add']">新增</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" style="margin-right: 9px"
                        @click="handleUpdate(scope.row)" v-hasPermi="['equipment:type:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['equipment:type:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      /> -->

        <!-- 添加或修改设备类型信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="故障类型编码" prop="typeCode">
                    <el-input v-model="form.typeCode" placeholder="请输入故障类型编码" style="width: 240px" />
                </el-form-item>
                <el-form-item label="故障类型名称" prop="typeName">
                    <el-input v-model="form.typeName" placeholder="请输入故障类型名称" style="width: 240px" />
                </el-form-item>
                <el-form-item label="描述" prop="remarks">
                    <el-input v-model="form.remarks" type="textarea" :rows="4" placeholder="请输入描述"
                        style="width: 240px" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {
        listEquipmentType,
        getEquipmentType,
        addEquipmentType,
        updateEquipmentType,
        delEquipmentType
    } from "@/api/equipment/malfunction";
    export default {
        name: "Malfunction",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 设备类型表格数据
                typeList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    // pageNum: 1,
                    // pageSize: 10,
                    // teamName: null,
                    // teamCode: null,
                    // teamWorkshop: null,
                    // usersList: null,
                    // status: null,
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    typeCode: [{ required: true, message: "状态不能为空", trigger: "blur" }],
                    typeName: [{ required: true, message: "状态不能为空", trigger: "blur" }]
                },
            };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询设备类型列表 */
            getList() {
                this.loading = true;
                listEquipmentType(this.queryParams).then((response) => {
                    this.typeList = response.data;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset(id, type) {
                if (type === 'add') {
                    this.form = {
                        typeId: null,
                        typeCode: null,
                        typeName: null,
                        remarks: null,
                        status: '0',
                        parentId: id || 0
                    };
                } else {
                    this.form = {
                        typeId: id || null,
                        typeCode: null,
                        typeName: null,
                        remarks: null,
                        status: '0',
                        parentId: null
                    };
                }
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            // handleQuery() {
            //   this.queryParams.pageNum = 1;
            //   this.getList();
            // },
            /** 重置按钮操作 */
            // resetQuery() {
            //   this.resetForm("queryForm");
            //   this.handleQuery();
            // },
            // 多选框选中数据
            // handleSelectionChange(selection) {
            //   this.ids = selection.map((item) => item.typeId);
            //   this.single = selection.length !== 1;
            //   this.multiple = !selection.length;
            // },
            /** 新增按钮操作 */
            handleAdd(row) {
                this.reset(row?.typeId, 'add');
                this.open = true;
                this.title = "添加设备类型";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset(row.typeId, 'updata');
                const typeId = row.typeId || this.ids;
                getEquipmentType(typeId).then((response) => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改设备类型";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate((valid) => {
                    if (valid) {
                        if (this.form.typeId !== null) {
                            updateEquipmentType(this.form).then((response) => {
                                if (response.code === 200) {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.getList();
                                }
                            });
                        } else {
                            addEquipmentType(this.form).then((response) => {
                                if (response.code === 200) {
                                    this.msgSuccess("新增成功");
                                    this.open = false;
                                    this.getList();
                                }
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const typeIds = row?.typeId || this.ids;
                this.$confirm(
                    '是否确认删除设备类型id为"' + typeIds + '"的数据项？',
                    "警告",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }
                )
                    .then(function () {
                        return delEquipmentType(typeIds);
                    })
                    .then(() => {
                        this.getList();
                        this.msgSuccess("删除成功");
                    })
                    .catch(function () { });
            },
            /** 导出按钮操作 */
            // handleExport() {
            //   const queryParams = this.queryParams;
            //   this.$confirm("是否确认导出所有设备类型数据项？", "警告", {
            //     confirmButtonText: "确定",
            //     cancelButtonText: "取消",
            //     type: "warning",
            //   })
            //     .then(function () {
            //       return exportTeam(queryParams);
            //     })
            //     .then((response) => {
            //       this.download(response.msg);
            //     })
            //     .catch(function () {});
            // },
        },
    };
</script>